import java.util.Scanner;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class minCover {
    public static int minCovered = Integer.MAX_VALUE;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int i = in.nextInt();
        int j = in.nextInt();
        int[][] cases = new int[i][j];
        for (int k = 0; k < i; k++) {
            for (int l = 0; l < j; l++) {
                cases[k][l] = in.nextInt();
            }
        }
        int goat = (1 << j) - 1;
        backtrack(cases, 0, 0, 0, goat);
        System.out.println(minCovered == Integer.MAX_VALUE ? -1 : minCovered);
    }
    public static void backtrack(int[][] cases, int index, int cover, int selected, int goat){
        if (cover == goat){
            if (selected < minCovered){
                minCovered = selected;
            }
            return;
        }
        if (index == cases.length){
            return;
        }
        backtrack(cases, index + 1, cover, selected, goat);
        int newC = cover;
        for (int j = 0; j < cases[index].length; j++) {
            if (cases[index][j] == 1) {
                newC |= (1 << (cases[index].length - 1 - j));
            }
        }
        backtrack(cases, index + 1, newC, selected + 1, goat);
    }
}
